package oneD.fem.visualize;

import inf.v3d.obj.CylinderSet;
import inf.v3d.obj.Text;
import inf.v3d.view.Viewer;

import java.util.ArrayList;

import oneD.fem.structure.Structure;

public class TrussVisualizer extends Visualizer {
	protected double areaScale_;

	public TrussVisualizer(Structure struct, Viewer view) {
		super(struct, view);
	}

	public void setAreaScale(double d) {
		this.areaScale_ = d;
	}

	@Override
	public double automaticScale() {
		double d_max = super.automaticScale();
		// finding a_max
		double a_max = 0;
		for (int i = 0; i < this.struct_.getNumberOfElements(); i++) {
			double a = this.struct_.getElement(i).getArea();
			if (a_max < a)
				a_max = a;
		}
		this.setAreaScale(0.01 * d_max / a_max);
		return d_max;
	}

	@Override
	public void drawElements() {
		/*
		 * draw elements in original position
		 */
		CylinderSet c = new CylinderSet();
		for (int i = 0; i < this.struct_.getNumberOfElements(); i++) {
			c.addCylinder(this.struct_.getElement(i).getNode(0).getPosition()
					.toArray(), this.struct_.getElement(i).getNode(1)
					.getPosition().toArray(), this.areaScale_
					* this.struct_.getElement(i).getArea());
		}
		this.view_.addObject3D(c);
	}

	// @Override
	// public void drawElements(int[] index, int color) {
	// /*
	// * draw set of elements with index in the "index" array with color
	// * "color"
	// */
	// CylinderSet c = new CylinderSet();
	// c.setColor(new Color(color));
	// for (int i = 0; i < index.length; i++) {
	// c.addCylinder(this.struct_.getElement(index[i]).getNode(0)
	// .getPosition().toArray(), this.struct_.getElement(index[i])
	// .getNode(1).getPosition().toArray(), this.areaScale_
	// * this.struct_.getElement(i).getArea());
	// }
	// this.view_.addObject3D(c);
	// }

	@Override
	public void drawElements(ArrayList<Integer> index, String color) {
		/*
		 * draw elements in original position
		 */
		/*
		 * draw set of elements with index in the "index" array with color
		 * "color"
		 */
		CylinderSet c = new CylinderSet();
		c.setColor(color);
		for (int i = 0; i < index.size(); i++) {
			int eId = (int) index.get(i).longValue();
			c.addCylinder(this.struct_.getElement(eId).getNode(0).getPosition()
					.toArray(), this.struct_.getElement(eId).getNode(1)
					.getPosition().toArray(), this.areaScale_
					* this.struct_.getElement(eId).getArea());
		}
		this.view_.addObject3D(c);
	}

	@Override
	public void drawDisplacedStructure() {
		CylinderSet c = new CylinderSet();
		c.setColor("cyan");
		for (int i = 0; i < this.struct_.getNumberOfElements(); i++) {
			c.addCylinder((this.struct_.getElement(i).getNode(0).getPosition()
					.add(this.struct_.getElement(i).getNode(0)
							.getDisplacement()
							.multiply(this.displacementScale_))).toArray(),
					(this.struct_.getElement(i).getNode(1).getPosition()
							.add(this.struct_.getElement(i).getNode(1)
									.getDisplacement()
									.multiply(this.displacementScale_)))
							.toArray(), this.areaScale_
							* this.struct_.getElement(i).getArea());
		}
		this.view_.addObject3D(c);
	}

	@Override
	public void showElementId() {
		for (int i = 0; i < this.struct_.getNumberOfElements(); i++) {
			Text t = new Text("e" + Integer.toString(i));
			double[] pos1 = this.struct_.getElement(i).getNode(0).getPosition()
					.toArray();
			double[] pos2 = this.struct_.getElement(i).getNode(1).getPosition()
					.toArray();
			t.setOrigin((pos1[0] + pos2[0]) / 2 + this.textHeight_,
					(pos1[1] + pos2[1]) / 2 + this.textHeight_,
					(pos1[2] + pos2[2]) / 2 + this.textHeight_);
			t.setHeight(this.textHeight_);
			t.setColor("magenta");
			this.view_.addObject3D(t);
		}
	}

	@Override
	public void showElementGroupId(ArrayList<Integer> elementGroupId) {
		for (int i = 0; i < this.struct_.getNumberOfElements(); i++) {
			Text t = new Text("g" + elementGroupId.get(i).toString());
			double[] pos1 = this.struct_.getElement(i).getNode(0).getPosition()
					.toArray();
			double[] pos2 = this.struct_.getElement(i).getNode(1).getPosition()
					.toArray();
			t.setOrigin((pos1[0] + pos2[0]) / 2 - this.textHeight_,
					(pos1[1] + pos2[1]) / 2 + this.textHeight_,
					(pos1[2] + pos2[2]) / 2 + this.textHeight_);
			t.setHeight(this.textHeight_);
			t.setColor("pink");
			this.view_.addObject3D(t);
		}
	}
}
